/* 
 * Copyright 2014 ShangDao.Ltd  All rights reserved.
 * SiChuan ShangDao.Ltd PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * 
 * @HomeFrament.java  2014-9-9 上午11:21:48 - Carson
 * @author Guzhu
 * @email:muyi126@163.com
 * @version 1.0
 */

package com.android.tiango.baba.fragment;

import java.util.ArrayList;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.scsd.tiango.R;
import com.android.support.httpclient.HttpCallBack;
import com.android.support.httpclient.HttpClientAsync;
import com.android.support.httpclient.HttpParams;
import com.android.tiango.baba.BaListFragment;
import com.android.tiango.baba.act.ActBaList;
import com.android.tiango.baba.act.ActCardDetail;
import com.android.tiango.baba.act.ActPublic;
import com.android.tiango.baba.adapter.HomeCenterAdapter;
import com.android.tiango.baba.adapter.HomeListAdapter;
import com.android.tiango.baba.cookie.HttpUrl;
import com.android.tiango.baba.entity.BarEntity;
import com.android.tiango.baba.entity.BarModel;
import com.android.tiango.baba.entity.HomeBannerEntity;
import com.android.tiango.baba.entity.ImageModel;
import com.android.tiango.baba.entity.IndexHotProductTopicsBean;
import com.android.tiango.baba.entity.IndexHotProductTopicsEntity;
import com.android.tiango.baba.entity.IndexHotTopicsEntity;
import com.android.tiango.baba.entity.SimpleTopicModel;
import com.android.tiango.baba.entity.SimpleUserModel;
import com.android.tiango.baba.util.Constants;
import com.android.tiango.baba.util.DensityUtil;
import com.android.tiango.baba.util.StringUtils;
import com.android.tiango.baba.util.TimeToUtil;
import com.android.tiango.baba.util.ToastUtils;
import com.android.tiango.baba.view.MeasureGridView;
import com.android.tiango.baba.view.MeasureListView;
import com.android.tiango.baba.view.PullListView.PullListViewListener;
import com.android.tiango.baba.view.RoundImageView;
import com.android.tiango.baba.view.TopDotPager;

public class FrHome extends BaListFragment<SimpleTopicModel> implements
		com.android.tiango.baba.BaListFragment.AddHeaderViewCallBack, PullListViewListener,
		TopDotPager.OnItemClickListener {

	private TopDotPager tp_home;
	private MeasureListView ml_listview;
	private MeasureGridView ml_gridview;
	private LinearLayout ll_qg;// 热门抢购
	private LinearLayout ll_rm;// 热门吧
	private TextView tv_tie;// 热门贴
	private HomeCenterAdapter homeCenterAdapter;

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		if (null != mTitleBar) {
			mTitleBar.setTitleBg(R.drawable.name_tiangou);
			mTitleBar.setImageBackGround(R.drawable.bg_title_blue);
		}
		tp_home.setImageLoader(mImageLoader);
		mListView.setDividerHeight(DensityUtil.dip2px(getActivity(), 15));
		showEmptyView();
		mListView.setPullListener(this);
		mListView.startOnRefresh();
	}

	private void showEmptyView() {
		setEmptyViewMsg("暂时没有帖子哟~");
		emptyListview.setPullListener(this);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		setHeaderViewCallBack(this);
		return super.onCreateView(inflater, container, savedInstanceState);
	}

	@Override
	protected View getItemView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (null == convertView) {
			holder = new ViewHolder();
			convertView = LayoutInflater.from(getActivity()).inflate(R.layout.item_actpost, null);
			holder.tv_acttitle = (TextView) convertView.findViewById(R.id.tv_acttitle);
			holder.tv_actinfo = (TextView) convertView.findViewById(R.id.tv_actinfo);

			holder.iv_acthead = (RoundImageView) convertView.findViewById(R.id.iv_acthead);
			holder.tv_acttitle = (TextView) convertView.findViewById(R.id.tv_acttitle);
			holder.tv_actinfo = (TextView) convertView.findViewById(R.id.tv_actinfo);
			holder.tv_acttime = (TextView) convertView.findViewById(R.id.tv_acttime);
			holder.iv_sex = (ImageView) convertView.findViewById(R.id.iv_sex);
			holder.iv_thelord = (ImageView) convertView.findViewById(R.id.iv_thelord);

			holder.tv_actcomments = (TextView) convertView.findViewById(R.id.tv_actcomments);
			holder.tv_acttoview = (TextView) convertView.findViewById(R.id.tv_acttoview);

			holder.iv_actre = (ImageView) convertView.findViewById(R.id.iv_actre);
			holder.iv_actj = (ImageView) convertView.findViewById(R.id.iv_actj);
			holder.iv_actd = (ImageView) convertView.findViewById(R.id.iv_actd);

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		final SimpleTopicModel bean = mListData.get(position);
		if (null != bean) {
			if (bean.getIsHot() == 1) {// 是否热点
				holder.iv_actre.setVisibility(View.VISIBLE);
			} else {
				holder.iv_actre.setVisibility(View.INVISIBLE);
			}
			if (bean.getIsTop() == 1) {// 是否指定
				holder.iv_actd.setVisibility(View.VISIBLE);
			} else {
				holder.iv_actd.setVisibility(View.INVISIBLE);
			}
			if (bean.getIsNice() == 1) {// 是否精
				holder.iv_actj.setVisibility(View.VISIBLE);
			} else {
				holder.iv_actj.setVisibility(View.INVISIBLE);
			}
			if (bean.getIsBarOwner() == 1) {// 是吧主
				holder.iv_thelord.setVisibility(View.VISIBLE);
			} else {
				holder.iv_thelord.setVisibility(View.INVISIBLE);
			}

			SimpleUserModel userBean = bean.getSimpleUser();
			if (null != userBean) {
				mImageLoader.loadImage(userBean.getAvatarUrl(), holder.iv_acthead);
				holder.tv_acttitle.setText(userBean.getNickName());
				if (userBean.getGender() == 1) {
					holder.iv_sex.setBackgroundResource(R.drawable.bg_sex);
				} else if (userBean.getGender() == 2) {
					holder.iv_sex.setBackgroundResource(R.drawable.bg_sex_girl);
				}
			} else {
				mImageLoader.loadImage("", holder.iv_acthead);
				holder.tv_acttitle.setText("");
			}
			holder.tv_actinfo.setText(bean.getTopicName());
			holder.tv_actcomments.setText(String.valueOf(bean.getReadNumber()));
			holder.tv_acttoview.setText(String.valueOf(bean.getReplyNumber()));
			holder.tv_acttime.setText(TimeToUtil.getSystemDayFormat(bean.getCreateTime()));

		}
		convertView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(getActivity(), ActCardDetail.class);
				intent.putExtra(Constants.BARID, bean.getBarId());
				intent.putExtra(Constants.TOPICID, bean.getTopicId());
				intent.putExtra(Constants.BARNAME, bean.getBarName());
				getActivity().startActivity(intent);
			}
		});
		return convertView;
	}

	class ViewHolder {
		private RoundImageView iv_acthead;
		private TextView tv_acttitle;
		private TextView tv_actinfo;
		private TextView tv_acttime;
		private ImageView iv_sex;
		private ImageView iv_thelord;
		private TextView tv_actcomments;
		private TextView tv_acttoview;

		private ImageView iv_actre;
		private ImageView iv_actj;
		private ImageView iv_actd;

	}

	// 热门贴
	private void loadPost() {
		HttpClientAsync client = HttpClientAsync.getInstance();
		client.setPrintLog(true);
		HttpParams param = new HttpParams();
		param.put("pageIndex", mPageIndex);
		param.put("pageSize", Constants.PAGE_SIZE);
		client.get(HttpUrl.getUrl(HttpUrl.HOTTOPICS), param, new HttpCallBack() {

			@Override
			public void onHttpSuccess(Object arg0) {
				IndexHotTopicsEntity entity = (IndexHotTopicsEntity) arg0;
				ArrayList<SimpleTopicModel> list = entity.getData();
				if (null != list) {
					mAdapter.putData(list);
				}
				mListView.onRefreshFinish();
				mListView.onLoadFinish();

			}

			@Override
			public void onHttpStarted() {

			}

			@Override
			public void onHttpFailure(Exception arg0, String arg1) {
				mListView.onRefreshFinish();
				mListView.onLoadFinish();
				if (mPageIndex > 1) {
					mPageIndex -= 1;
				}
				if (!StringUtils.isEmpty(arg1)) {
					ToastUtils.showMessage(arg1);
				}
			}
		}, IndexHotTopicsEntity.class);

	}

	@Override
	public void addHeaderView() {
		View header = LayoutInflater.from(getActivity()).inflate(R.layout.header_fragment_main,
				null);
		tp_home = (TopDotPager) header.findViewById(R.id.tp_home);
		tp_home.setOnItemClickListener(this);
		ll_qg = (LinearLayout) header.findViewById(R.id.ll_qg);
		ml_listview = (MeasureListView) header.findViewById(R.id.ml_listview);

		ll_rm = (LinearLayout) header.findViewById(R.id.ll_rm);
		ml_gridview = (MeasureGridView) header.findViewById(R.id.ml_gridview);
		ml_gridview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				if (null != homeCenterAdapter) {
					Intent intent = new Intent(getActivity(), ActBaList.class);
					intent.putExtra(Constants.BARID, homeCenterAdapter.getItem(position));
					getActivity().startActivity(intent);
				}
			}
		});
		tv_tie = (TextView) header.findViewById(R.id.tv_tie);
		mListView.addHeaderView(header);
	}

	@Override
	public void onPullRefresh() {
		mPageIndex = 1;
		loadPost();
		loadBanner();
		loadHotPanicBuying();
		loadIndexHotBars();

	}

	@Override
	public void onPullLoadMore() {
		mPageIndex += 1;
		loadPost();
	}

	@Override
	public void onItemClickListener(int position) {
		// showActivity(ActMyDatum.class);
		showActivity(ActPublic.class);

	}

	// banner
	private void loadBanner() {
		HttpClientAsync client = HttpClientAsync.getInstance();
		client.get(HttpUrl.getUrl(HttpUrl.HOMEBANNER), null, new HttpCallBack() {

			@Override
			public void onHttpSuccess(Object arg0) {
				HomeBannerEntity entity = (HomeBannerEntity) arg0;
				if (null != entity) {
					ArrayList<ImageModel> datas = entity.getData();
					if (null != datas) {
						tp_home.setVisibility(View.VISIBLE);
						tp_home.updateViews(datas);
					} else {
						tp_home.setVisibility(View.GONE);
					}

				}
			}

			@Override
			public void onHttpStarted() {

			}

			@Override
			public void onHttpFailure(Exception arg0, String arg1) {
				if (!StringUtils.isEmpty(arg1)) {
					ToastUtils.showMessage(arg1);
				}
			}
		}, HomeBannerEntity.class);//
	}

	// 热门抢购
	private void loadHotPanicBuying() {
		HttpClientAsync client = HttpClientAsync.getInstance();
		client.get(HttpUrl.getUrl(HttpUrl.HOMEHOTTOPICS), null, new HttpCallBack() {

			@Override
			public void onHttpFailure(Exception arg0, String arg1) {
				if (!StringUtils.isEmpty(arg1)) {
					ToastUtils.showMessage(arg1);
				}
			}

			@Override
			public void onHttpStarted() {

			}

			@Override
			public void onHttpSuccess(Object arg0) {
				if (null != arg0) {
					IndexHotProductTopicsEntity entity = (IndexHotProductTopicsEntity) arg0;
					ArrayList<IndexHotProductTopicsBean> datas = entity.getData();
					if (null != datas && datas.size() > 0) {
						ll_qg.setVisibility(View.VISIBLE);
						HomeListAdapter homeListAdapter = new HomeListAdapter(getActivity(), datas);
						ml_listview.setAdapter(homeListAdapter);
					} else {
						ll_qg.setVisibility(View.GONE);
					}
				}
			}

		}, IndexHotProductTopicsEntity.class);//
	}

	// 热门吧
	private void loadIndexHotBars() {
		HttpClientAsync client = HttpClientAsync.getInstance();
		client.get(HttpUrl.getUrl(HttpUrl.INDEXHOTBARS), null, new HttpCallBack() {

			@Override
			public void onHttpFailure(Exception arg0, String arg1) {
				if (!StringUtils.isEmpty(arg1)) {
					ToastUtils.showMessage(arg1);
				}
			}

			@Override
			public void onHttpStarted() {

			}

			@Override
			public void onHttpSuccess(Object arg0) {
				if (null != arg0) {
					BarEntity entity = (BarEntity) arg0;
					ArrayList<BarModel> datas = entity.getData();
					if (null != datas && datas.size() > 0) {
						ll_rm.setVisibility(View.VISIBLE);
						homeCenterAdapter = new HomeCenterAdapter(getActivity(), datas);
						ml_gridview.setAdapter(homeCenterAdapter);
					} else {
						ll_rm.setVisibility(View.GONE);
					}
				}
			}

		}, BarEntity.class);
	}

}
